#!/usr/bin/perl -w
#
# @param $1 install home
# @param $2 delimiter
# @param $3 host
# @param $4 port
# @param $5 username
# @param $6 password
#
# -- 05/27/2009 Created by Zhixin Li
# -- Used to check if innodb-status-file parameter was set
# -- changed output from key|value to value, 06/26/2009 Modified by Zhixin Li
use strict;

my ($MYSQL_HOME,$DELIM,$HOST,$PORT,$USER,$PASS) = @ARGV;
my $SQL1="show variables like 'pid_file'";
my $SQL2="show variables like 'datadir'";

my $PARAM="";
$PARAM="$PARAM -h\"$HOST\"" if (defined($HOST) && $HOST ne "");
$PARAM="$PARAM -P\"$PORT\"" if (defined($PORT) && $PORT ne "");
$PARAM="$PARAM -u\"$USER\"" if (defined($USER) && $USER ne "");
$PARAM="$PARAM -p\"$PASS\"" if (defined($PASS) && $PASS ne "");

my $MYSQL="$MYSQL_HOME/bin/mysql";
my $MYSQLADMIN="$MYSQL_HOME/bin/mysqladmin"; 

if ($^O =~ m/MSWin32/) {
  $MYSQL =~ s/\//\\/g;
  $MYSQLADMIN =~ s/\//\\/g;
}
# Check if mysqld is alive
my $retmsg=`$MYSQLADMIN $PARAM ping 2>&1`;
chomp($retmsg);
if ( "$retmsg" ne "mysqld is alive" ) {
    print "em_error=Status${DELIM}Down\n";
    exit 1;
}

my $pidfile=`$MYSQL -N -s $PARAM --batch -e "$SQL1"`;
my $datadir=`$MYSQL -N -s $PARAM --batch -e "$SQL2"`;
my $file = substr($pidfile,index($pidfile,"\t")+1,length($pidfile)-index($pidfile,"\t")+1);
my $dir = substr($datadir,index($datadir,"\t")+1,length($datadir)-index($datadir,"\t")+1);
my $reads;
chomp($file);
chomp($dir);

if (-s "$file") {
    open (FILES, "$file") or die "Couldn't open $file for reading: $!\n"; 
    $/=""; 
    $reads = <FILES>; 
    close(FILES); 
    $/="\n";
}
chomp($reads);

#   column:  v_innodbstatusfile
#   value:   0 exists, 1 non-exists
my $innofile = "${dir}innodb_status.$reads";
if ( -e "$innofile" ) {
    print "em_result=0\n";
} else {
    print "em_result=1\n";
}
exit 0;
